﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AutoMapper;
using H2.NET.Web.Workflow.Services;
using H2.NET.Business.Core;
using H2.NET.Web.Management.Config.Service;
using H2.NET.Web.Management.Config.Model;

namespace H2.NET.Web.Management.Config
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {

        private readonly ConfigService _configService = new ConfigService();
        public void ProcessRequest(HttpContext context)
        {
            var action = context.Request.QueryString["action"];
            int pId = 0;
            String ss = context.Request.QueryString["pId"];
            if (!context.Request.QueryString["pId"].Equals("")) 
            {
                pId = int.Parse(context.Request.QueryString["pId"]);
            }
            
            switch (action)
            {
                case "GetData":
                    GetData(context, pId);
                    break;
                default:
                    break;

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private void GetData(HttpContext context,int pId)
        {
            Mapper.CreateMap<t_smd_Config, H2.NET.Web.Workflow.Models.Config>();

            var configs =
                H2.NET.Web.Workflow.Helpers.ConvertHelper<t_smd_Config>.ConvertToList(
                    _configService.GetList("").Tables[0]);

            var list = new List<H2.NET.Web.Workflow.Models.Config>();

            

            var parents = configs.Where(c => c.ParentID == null).OrderBy(c=>c.Seq).ToList();

            if (pId != 0)
            {
                parents = configs.Where(c => c.ParentID == pId).OrderBy(c => c.Seq).ToList();
                
            }
            string name = context.Request.QueryString.Get("name");
            if (!string.IsNullOrEmpty(name))
            {
                parents = configs.Where(c => c.Name.Contains(name)).OrderBy(c => c.Seq).ToList();
            }
            
            foreach (var config in parents)
            {
                var model = Mapper.Map<t_smd_Config, H2.NET.Web.Workflow.Models.Config>(config);

                var children = configs.Where(c => c.ParentID == model.ConfigId).ToList();

                if (children.Any())
                {
                    if (model.Children == null)
                        model.Children = new List<H2.NET.Web.Workflow.Models.Config>();
                    model.Children.AddRange(CovertConfigs(configs, children));
                }

                list.Add(model);
            }

            var setting = new Newtonsoft.Json.JsonSerializerSettings
            {
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
            };

            list = list.OrderBy(t => t.ConfigId).ToList();

            var perms = new H2.NET.Web.Workflow.Models.GridData<H2.NET.Web.Workflow.Models.Config>(list);

            var content = Newtonsoft.Json.JsonConvert.SerializeObject(perms, setting);


            context.Response.Clear();
            context.Response.ContentType = "text/plain";
            context.Response.Write(content.Replace("Children", "children"));
            context.Response.End();
        }

        private List<H2.NET.Web.Workflow.Models.Config> CovertConfigs(List<t_smd_Config> allconfigs, List<t_smd_Config> configs)
        {
            var list = new List<H2.NET.Web.Workflow.Models.Config>();
            foreach (var config in configs)
            {
                var model = Mapper.Map<t_smd_Config, H2.NET.Web.Workflow.Models.Config>(config);
                var children = allconfigs.Where(t => t.ParentID == model.ConfigId).OrderBy(t=> t.Seq).ToList();
                 
                if (children.Any())
                {
                    if (model.Children == null)
                        model.Children = new List<H2.NET.Web.Workflow.Models.Config>();
                    model.Children.AddRange(CovertConfigs(allconfigs, children));
                }

                list.Add(model);
            }

            return list;
        }
    }
    
}